package vip.xiaonuo.facedoor.modular.reactor.listener;

import static reactor.event.selector.Selectors.$;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reactor.core.Reactor;
import reactor.event.Event;
import reactor.function.Consumer;
import vip.xiaonuo.facedoor.modular.memberdevice.entity.MemberDevice;
import vip.xiaonuo.facedoor.modular.memberdevice.service.MemberDeviceService;
import vip.xiaonuo.facedoor.modular.topic.EventTopic;

/**
 * 梯控监听访客删除事件做人员变更
 */
@Service
@Slf4j
public class FacedoorMemberUpdateCardListener implements Consumer<Event<Long>>, InitializingBean {

    @Autowired
    private Reactor reactor;

    @Autowired
    private MemberDeviceService deviceService;

    @Override
    public void accept(Event<Long> event) {
        deviceService.update(new LambdaUpdateWrapper<MemberDevice>().set(MemberDevice::getStatus, 2)
                .eq(MemberDevice::getMemberId, event.getData()));
    }

    public void afterPropertiesSet() {
        reactor.on($(EventTopic.MEMBER_UPDATE_CARD), this);
    }
}
